<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('主订单列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>订单号：</label>
                            <input type="text" name="orderId"/>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="status" th:with="type=${@dict.getType('pay_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:order:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<!-- 订单详情弹窗 -->
<div class="modal fade" id="orderDetailModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">订单详情</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="box box-info">
                            <div class="box-header with-border">
                                <h3 class="box-title">订单基本信息</h3>
                            </div>
                            <div class="box-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <p><strong>订单号：</strong><span id="detail-orderId"></span></p>
                                        <p><strong>总金额：</strong><span id="detail-totalAmount"></span></p>
                                        <p><strong>押金：</strong><span id="detail-depositAmount"></span></p>
                                    </div>
                                    <div class="col-md-6">
                                        <p><strong>起租时间：</strong><span id="detail-leaseStart"></span></p>
                                        <p><strong>应还时间：</strong><span id="detail-leaseEnd"></span></p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="box box-info">
                            <div class="box-header with-border">
                                <h3 class="box-title">订单明细</h3>
                            </div>
                            <div class="box-body">
                                <table class="table table-bordered">
                                    <thead>
                                    <tr>
                                        <th>商品名称</th>
                                        <th>图片</th>
                                        <th>数量</th>
                                        <th>日租金</th>
                                        <th>租赁天数</th>
                                    </tr>
                                    </thead>
                                    <tbody id="detail-items">
                                    <!-- 订单明细内容将通过JS动态填充 -->
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:order:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:order:remove')}]];
    var statusDatas = [[${@dict.getType('pay_status')}]];
    var prefix = ctx + "system/order";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "主订单",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '订单号',
                    visible: false
                },
                {
                    field: 'userName',
                    title: '租户'
                },
                {
                    field: 'totalAmount',
                    title: '订单总额'
                },
                {
                    field: 'depositAmount',
                    title: '总押金'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'leaseStart',
                    title: '起租时间'
                },
                {
                    field: 'leaseEnd',
                    title: '应还时间'
                },
                {
                    field: 'actualReturn',
                    title: '实际归还时间'
                },
                {
                    field: 'overdueFee',
                    title: '超期费用'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="showOrderDetail(\'' + row.id + '\')"><i class="fa fa-search"></i>查看详情</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 显示订单详情
    function showOrderDetail(orderId) {
        $.modal.loading("正在加载订单详情，请稍候...");
        $.ajax({
            url: prefix + "/queryOrderDetails",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify({orderId: orderId}),
            success: function(result) {
                $.modal.closeLoading();
                if (result.code === 0) {
                    var data = result.data;
                    // 填充基本信息
                    $("#detail-orderId").text(data.orderId);
                    $("#detail-totalAmount").text(data.totalAmount);
                    $("#detail-depositAmount").text(data.depositAmount);
                    $("#detail-leaseStart").text(data.leaseStart);
                    $("#detail-leaseEnd").text(data.leaseEnd);

                    // 填充订单明细
                    var itemsHtml = '';
                    $.each(data.orderDetails, function(index, item) {
                        itemsHtml += '<tr>' +
                            '<td>' + item.itemName + '</td>' +
                            '<td><img src="' + item.imgUrl + '" style="max-height:50px;max-width:50px;"></td>' +
                            '<td>' + item.quantity + '</td>' +
                            '<td>' + item.dailyRate + '</td>' +
                            '<td>' + item.days + '</td>' +
                            '</tr>';
                    });
                    $("#detail-items").html(itemsHtml);

                    // 显示弹窗
                    $("#orderDetailModal").modal("show");
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function() {
                $.modal.closeLoading();
                $.modal.alertError("加载订单详情失败");
            }
        });
    }
</script>
</body>
</html>